package com.course.aspects;
/*
@author By 林中小鸟
@package  com.self.introduction.aspects
@create 2022 - 01 - 2022\1\10 0010
@QQ: 357182695
@Em: themooncss@gmail.com
*/

import com.course.component.aspects.LogAspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Aspect
@Component
public class OssAspect {

    private final static Logger LOG = LoggerFactory.getLogger(LogAspect.class);

    /** 定义一个切点 */
    @Pointcut("execution(public * com.course.service.FilesUploadService.storeFile(..))")
    public void ossPointcut() {}

    @AfterReturning(value = "ossPointcut()",returning = "result")
    public void doAfter(JoinPoint joinPoint,Object result) throws Throwable {
        if(result instanceof HashMap){
            LOG.info("------------- OSS上传信息 -------------");
            LOG.info("文件名称是:" +  "/" + (String) ((HashMap<?, ?>) result).get("fileName"));
            LOG.info("------------- OSS上传完成 -------------");
        }
    }

    private String handlerParameter(ProceedingJoinPoint point){
        StringBuilder stringBuilder = new StringBuilder();
        MethodSignature methodSignature = (MethodSignature)point.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Class[] parameterTypes = methodSignature.getParameterTypes();
        Object[] args = point.getArgs();
        int i=0;
        for (Object pojo : args){
            stringBuilder.append("parameterName:").append(parameterNames[i]);
            stringBuilder.append("parameterType:").append(parameterTypes[i]);
            stringBuilder.append("parameterValue:").append(pojo);
        }
        return stringBuilder.toString();
    }
}
